//
//  XS_VPSlider.swift
//  XueDao
//
//  Created by 韩云智 on 2020/12/27.
//

import UIKit
import RxSwift
import RxCocoa

class XS_VPSlider: UISlider {
    lazy var disposeBag = DisposeBag()
    lazy var progressView = loadProgressView()
    lazy var controlEventPS = PublishSubject<UIControl.Event>()
    
    // MARK: 处理播放进度条
    func upSliderLoading(_ progress: Float) {
        progressView.setProgress(progress, animated: true)
    }
    func upSlider(_ current: Int64, total: Int64) {
        setValue(Float(current) / Float(total), animated: true)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        upView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func upView () {
        maximumValue = 1
        maximumTrackTintColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 0.1)
        minimumTrackTintColor = #colorLiteral(red: 0.7215686275, green: 0.6745098039, blue: 0.6588235294, alpha: 1)
        
        addSubview(progressView)
        progressView.snp.makeConstraints { maker in
            maker.left.right.equalToSuperview()
            maker.centerY.equalToSuperview().offset(0.5)
            maker.height.equalTo(2)
        }
        sendSubviewToBack(progressView)
        
        setThumbImage(getImg(#colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)), for: .normal)
        rx.controlEvent(.touchDown).asDriver()
            .map { .touchDown }
            .drive(controlEventPS)
            .disposed(by: disposeBag)
        rx.controlEvent([.touchUpInside, .touchUpOutside, .touchDownRepeat, .touchCancel]).asDriver()
            .map { .touchUpInside }
            .drive(controlEventPS)
            .disposed(by: disposeBag)
        rx.controlEvent(.valueChanged).asDriver()
            .map { .valueChanged }
            .drive(controlEventPS)
            .disposed(by: disposeBag)
    }
    private func loadProgressView () -> UIProgressView {
        let progress = UIProgressView(progressViewStyle: .default)
        progress.progressTintColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 0.1)
        progress.trackTintColor = .clear
        progress.layer.masksToBounds = true
        progress.layer.cornerRadius = 1
        return progress
    }
//    override func thumbRect(forBounds bounds: CGRect, trackRect rect: CGRect, value: Float) -> CGRect {
//        var rect = rect
//        rect.origin.x -= 10
//        rect.size.width += 20
//        return super.thumbRect(forBounds: bounds, trackRect: rect, value: value).insetBy(dx: 10, dy: 10)
//    }
    func getImg(_ color: UIColor) -> UIImage? {
        let rect = CGRect(x: 0, y: 0, width: 4, height: 4)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()
        context?.addArc(center: CGPoint(x: 2, y: 2), radius: 2, startAngle: 0, endAngle: .pi*2, clockwise: true)
        context?.setFillColor(color.cgColor)
        context?.fillPath()
        let img = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return img
    }
}
